
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;

public class Midlet extends MIDlet implements CommandListener, ItemCommandListener {

    private HeroGame heroGame;
    private HeroGame1 heroGame1;
    private HeroGame2 heroGame2;
    private HeroGame3 heroGame3;
    private Form mMenu;
    private Form mSetting;
    private List mHighScore;
    private Form mHelp;
    private Form mAbout;
    private Form mGameOver;
    private Form mTramittedform;
    private Form mEnGame;
    private StringItem mSNewGame;
    private StringItem mSSetting;
    private StringItem mSHighScore;
    private StringItem mSHelp;
    private StringItem mSAbout;
    private Command CMD_EXIT;
    private Command CMD_SELECT;
    private Command CMD_BACK;
    private Command CMD_TRY;
    private Command CMD_CON;
    private Command CMD_CHO;
    private int level = 0;
    private int isSound = 0;
    private int score = 0;
    private ChoiceGroup choice;
    private RecordStore mStore;

    public Midlet() {

        mMenu = new Form("Menu Game");
        mSetting = new Form("Setting");
        mHighScore = new List("High score", List.EXCLUSIVE);
        mHelp = new Form("Help");
        mAbout = new Form("About");
        mGameOver = new Form("Game Over !!");
        mTramittedform = new Form("Congratulation");
        mEnGame = new Form("End Game");
        mSNewGame = new StringItem("", "New Game", StringItem.BUTTON);
        mSSetting = new StringItem("", "    Setting    ", StringItem.BUTTON);
        mSHighScore = new StringItem("", "High Score", StringItem.BUTTON);
        mSHelp = new StringItem("", "     Help     ", StringItem.BUTTON);
        mSAbout = new StringItem("", "    About    ", StringItem.BUTTON);
        String[] yesno = {"Yes", "No"};
        choice = new ChoiceGroup("Sound", ChoiceGroup.EXCLUSIVE, yesno, null);
        CMD_EXIT = new Command("Exit", Command.SCREEN, 0);
        CMD_SELECT = new Command("Select", Command.SCREEN, 0);
        CMD_BACK = new Command("Back", Command.SCREEN, 0);
        CMD_TRY = new Command("Try again", Command.SCREEN, 0);
        CMD_CON = new Command("Continute", Command.SCREEN, 0);
        CMD_CHO = new Command("Ok", Command.SCREEN, 0);



        try {
            mStore = RecordStore.openRecordStore("mStore", true);
            System.out.println("OK -------------------------------------------------------------------------------- OK");
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        }
    }

    public void createMainMenu() {
        mSNewGame.setDefaultCommand(CMD_SELECT);
        mSNewGame.setItemCommandListener(this);
        mSNewGame.setLayout(StringItem.LAYOUT_CENTER);

        mSSetting.setDefaultCommand(CMD_SELECT);
        mSSetting.setItemCommandListener(this);
        mSSetting.setLayout(StringItem.LAYOUT_CENTER);

        mSHighScore.setDefaultCommand(CMD_SELECT);
        mSHighScore.setItemCommandListener(this);
        mSHighScore.setLayout(StringItem.LAYOUT_CENTER);

        mSHelp.setDefaultCommand(CMD_SELECT);
        mSHelp.setItemCommandListener(this);
        mSHelp.setLayout(StringItem.LAYOUT_CENTER);

        mSAbout.setDefaultCommand(CMD_SELECT);
        mSAbout.setItemCommandListener(this);
        mSAbout.setLayout(StringItem.LAYOUT_CENTER);

        mMenu.append(mSNewGame);
        mMenu.append("\n");
        mMenu.append(mSSetting);
        mMenu.append("\n");
        mMenu.append(mSHighScore);
        mMenu.append("\n");
        mMenu.append(mSHelp);
        mMenu.append("\n");
        mMenu.append(mSAbout);

        mMenu.addCommand(CMD_EXIT);
        mMenu.addCommand(CMD_SELECT);

        mMenu.setCommandListener(this);
        Display.getDisplay(this).setCurrent(mMenu);
    }

    public void createMenuNewGame() {
        heroGame = new HeroGame(this, this.mMenu);
        heroGame.addCommand(CMD_BACK);
        heroGame.setCommandListener(this);
        if (choice.getSelectedIndex() != 0) {
            heroGame.audio.stop();
        }
        Display.getDisplay(this).setCurrent(heroGame);
    }

    public void createMenuSetting() {
        mSetting.deleteAll();

        mSetting.append(choice);
        mSetting.addCommand(CMD_BACK);
        mSetting.setCommandListener(this);
        Display.getDisplay(this).setCurrent(mSetting);
    }

    public void createMenuHighScore() {
        mHighScore.deleteAll();
        String high = null;
        try {
            if (mStore.getRecord(1) != null) {
                high = new String(mStore.getRecord(1));
            }
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        }
        if (high != null) {
            mHighScore.append(high, null);
        } else {
            mHighScore.append("No high score", null);
        }
        mHighScore.addCommand(CMD_BACK);
        mHighScore.setCommandListener(this);
        Display.getDisplay(this).setCurrent(mHighScore);
    }

    public void createMenuHelp() {
        mHelp.append("How to play");
        StringItem text = new StringItem(null, "Press arrows right, left, up , dow to move character ward off rivals to win ");
        text.setLayout(StringItem.LAYOUT_CENTER);
        mHelp.append(text);
        mHelp.addCommand(CMD_BACK);
        mHelp.setCommandListener(this);
        Display.getDisplay(this).setCurrent(mHelp);
    }

    public void createMenuAbout() {
        StringItem copy = new StringItem(null, "Copyright AMD1101 - Group 1");
        StringItem thank = new StringItem(null, "Special thank to Pham Xuan Lam");
        copy.setLayout(StringItem.LAYOUT_CENTER);
        thank.setLayout(StringItem.LAYOUT_CENTER);
        mAbout.append(copy);
        mAbout.append(thank);
        mAbout.addCommand(CMD_BACK);
        mAbout.setCommandListener(this);
        Display.getDisplay(this).setCurrent(mAbout);
    }

    public void createMenuEndGame() {
        String scoreS = score + 100 + "";
        try {
            mStore.setRecord(1, scoreS.getBytes(), scoreS.getBytes().length, 0);
            System.out.println("0k-----------------------------------------------------------------------------------------0k");
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        }
        mEnGame.addCommand(CMD_BACK);
        StringItem newString = new StringItem(null, "Congratulation");
        newString.setLayout(StringItem.LAYOUT_CENTER);
        StringItem thank = new StringItem(null, "Thank for play game, press back to try again");
        mEnGame.append(newString);
        mEnGame.append("\n");
        mEnGame.append(thank);
        mEnGame.setCommandListener(this);
        Display.getDisplay(this).setCurrent(mEnGame);
    }

    public void createGameOver() {
        mGameOver.deleteAll();
        mGameOver.append("\n\n\n\n\n");
        StringItem game = new StringItem("", "Game over");
        game.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE));
        game.setLayout(StringItem.LAYOUT_CENTER);
        mGameOver.append(game);
        mGameOver.setCommandListener(this);
        mGameOver.addCommand(CMD_BACK);
        mGameOver.addCommand(CMD_TRY);
        Display.getDisplay(this).setCurrent(mGameOver);
    }

    public void createTranmitted() {
        if (heroGame != null) {
            score += 100 + heroGame.score;
        }
        if (heroGame1 != null) {
            score += 100 + heroGame1.score;
        }
        if (heroGame2 != null) {
            score += 100 + heroGame2.score;
        }
        if (heroGame3 != null) {
            score += 100 + heroGame3.score;
        }
        String scoreS = score + "";
        if (level == 0) {
            try {
                int a = mStore.addRecord(scoreS.getBytes(), 0, scoreS.getBytes().length);
                String string = new String(mStore.getRecord(1));
            } catch (RecordStoreException ex) {
                ex.printStackTrace();
            }
        } else {
            try {
                mStore.setRecord(1, scoreS.getBytes(), 0, scoreS.getBytes().length);
            } catch (RecordStoreException ex) {
                ex.printStackTrace();
            }
        }
        mTramittedform.deleteAll();
        mTramittedform.append("\n\n\n\n\n");
        StringItem game = new StringItem("", "Congratulation");
        StringItem score = new StringItem("", ("Your score " + scoreS));
        game.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE));
        game.setLayout(StringItem.LAYOUT_CENTER);
        score.setLayout(StringItem.LAYOUT_CENTER);
        score.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE));
        mTramittedform.append(game);
        mTramittedform.append("\n");
        mTramittedform.append(score);
        mTramittedform.addCommand(CMD_BACK);
        mTramittedform.addCommand(CMD_CON);
        mTramittedform.setCommandListener(this);
        Display.getDisplay(this).setCurrent(mTramittedform);
    }

    public void startApp() {
        createMainMenu();
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void commandAction(Command c, Displayable d) {
        System.out.println("click");
        if (c == CMD_BACK) {
            if (heroGame != null) {
                heroGame.audio.stop();
            }
            if (heroGame1 != null) {
                heroGame1.audio.stop();
            }
            if (heroGame2 != null) {
                heroGame2.audio.stop();
            }
            Display.getDisplay(this).setCurrent(mMenu);
        } else {
            if (c == CMD_TRY) {
                if (heroGame != null) {
                    heroGame.audio.stop();
                }
                if (heroGame1 != null) {
                    heroGame1.audio.stop();
                }
                if (heroGame2 != null) {
                    heroGame2.audio.stop();
                }
                if (level == 0) {
                    heroGame = new HeroGame(this, this.mMenu);
                    heroGame.addCommand(CMD_BACK);
                    heroGame.setCommandListener(this);
                    if (choice.getSelectedIndex() != 0) {
                        heroGame.audio.stop();
                        System.out.println("Stop");
                    }
                    Display.getDisplay(this).setCurrent(heroGame);
                }
                if (level == 1) {
                    heroGame1 = new HeroGame1(this, mMenu);
                    heroGame1.addCommand(CMD_BACK);
                    heroGame1.setCommandListener(this);
                    if (choice.getSelectedIndex() != 0) {
                        heroGame1.audio.stop();
                    }
                    Display.getDisplay(this).setCurrent(heroGame1);
                }
                if (level == 2) {
                    heroGame2 = new HeroGame2(this, mMenu);
                    heroGame2.addCommand(CMD_BACK);
                    heroGame2.setCommandListener(this);
                    if (choice.getSelectedIndex() != 0) {
                        heroGame2.audio.stop();
                    }
                    Display.getDisplay(this).setCurrent(heroGame2);
                }
                if (level == 3) {
                    heroGame3 = new HeroGame3(this, mMenu);
                    heroGame3.addCommand(CMD_BACK);
                    heroGame3.setCommandListener(this);
                    if (choice.getSelectedIndex() != 0) {
                        heroGame3.audio.stop();
                    }
                    Display.getDisplay(this).setCurrent(heroGame3);
                }


            } else {
                if (c == CMD_CON) {
                    level++;
                    if (level == 1) {
                        heroGame1 = new HeroGame1(this, mMenu);
                        heroGame1.addCommand(CMD_BACK);
                        heroGame1.setCommandListener(this);
                        if (choice.getSelectedIndex() != 0) {
                            heroGame1.audio.stop();
                        }
                        Display.getDisplay(this).setCurrent(heroGame1);
                    }
                    if (level == 2) {
                        heroGame2 = new HeroGame2(this, mMenu);
                        heroGame2.addCommand(CMD_BACK);
                        heroGame2.setCommandListener(this);
                        if (choice.getSelectedIndex() != 0) {
                            heroGame2.audio.stop();
                        }
                        Display.getDisplay(this).setCurrent(heroGame2);
                    }
                    if (level == 3) {
                        heroGame3 = new HeroGame3(this, mMenu);
                        heroGame3.addCommand(CMD_BACK);
                        heroGame3.setCommandListener(this);
                        if (choice.getSelectedIndex() != 0) {
                            heroGame3.audio.stop();
                        }
                        Display.getDisplay(this).setCurrent(heroGame3);
                    }
                    if (level == 4) {
                        createMenuEndGame();
                    }
                }
            }
        }

        if (c == CMD_CHO) {
            if (choice.getSelectedIndex() == 0) {
            }
        }

    }

    public void commandAction(Command c, Item item) {

        if (item.equals(mSNewGame)) {
            createMenuNewGame();
        } else {
            if (item.equals(mSSetting)) {
                createMenuSetting();
            } else {
                if (item.equals(mSHighScore)) {
                    createMenuHighScore();
                } else {
                    if (item.equals(mSHelp)) {
                        createMenuHelp();
                    } else {
                        createMenuAbout();
                        //createMenuEndGame();
                    }
                }
            }
        }

    }
}
